/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Actors;

import graphics.RenderInstruction;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Shape;
import stealth.GameManager;
import stealth.Main;

/**
 * The shot actor is actor produced by the PlayerActor in response
 * to a fire command. When this actor collides with an EnemyActor, the
 * EnemyActor dies.
 * @author Joshua
 */
public class ShotActor extends Actor {

    public ShotActor(PlayerActor parent) {
        this.parent = parent;
        x = parent.x;
        y = parent.y;
        ActorManager.addToCollisionDetect(this);
    }
    private PlayerActor parent;

    /*
     * The renderer needs to take into account if the shot is destroyed
     */
    public boolean destroyed = false;

    @Override
    public void act() {
        x += 6;
        if (x > Main.game.getWidth()) {
            destroyed = true;
            ActorManager.removeFromCollisionDetection(this);
        }
    }

    @Override
    public RenderInstruction render() {
        return new ShotActorRender(this);
    }

    @Override
    public void collide(Actor collidedWith) {
        if (collidedWith instanceof EnemyActor) {
            ((EnemyActor) collidedWith).die = true;
            parent.score++;
            GameManager.currentScore++;
            destroyed = true;
        }
    }

    @Override
    public Shape[] collidableArea() {
        Polygon p = new Polygon();
        if (!destroyed) {
            p.addPoint((int) x, (int) y);
            p.addPoint((int) x, (int) y + 2);
            p.addPoint((int) x + 10, (int) y);
            p.addPoint((int) x + 10, (int) y + 2);
        }
        Shape[] s = new Shape[1];
        s[0] = p;
        return s;
    }
}

class ShotActorRender extends RenderInstruction {

    ShotActor s;

    public ShotActorRender(ShotActor s) {
        this.s = s;
    }

    @Override
    public void render(Graphics2D g) {
        g.setColor(Color.red);
        g.fill(s.collidableArea()[0]);
    }
}
